package com.heiyu.controller;

import com.alibaba.fastjson.JSON;
import com.heiyu.entity.AdminUserInfo;
import com.heiyu.utils.CacheKey;
import com.heiyu.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminBaseController {

    @Autowired
    protected RedisUtils redisUtils;

    public AdminUserInfo getUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        if(StringUtils.isEmpty(token)){
            return null;
        }
        AdminUserInfo userInfo = JSON.parseObject(redisUtils.get(CacheKey.ADMIN_TOKEN +token),AdminUserInfo.class);
        return userInfo;
    }

    protected void setHeader(String key,String value){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.addHeader(key,value);
    }
}

